package com.xu.system.model.convert;

import com.xu.mybatis.model.PageInfo;
import com.xu.security.model.CurrentRole;
import com.xu.system.model.entity.SysRole;
import com.xu.system.model.pojo.role.AddRoleReq;
import com.xu.system.model.pojo.role.RoleSimpleVo;
import com.xu.system.model.pojo.role.RoleVo;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;
import java.util.Map;

/**
 * 角色pojo转换
 *
 * @Author: 许乾伟
 * @Date: 2024/4/1 15:55
 */
@Mapper(componentModel = "spring")
public interface RoleConvert {
    @Mappings({
            @Mapping(target = "createBy", expression = "java(com.xu.security.utils.SecurityUtils.getUserId())"),
            @Mapping(target = "createTime", expression = "java(java.time.LocalDateTime.now())"),
            @Mapping(target = "updateBy", expression = "java(com.xu.security.utils.SecurityUtils.getUserId())"),
            @Mapping(target = "updateTime", expression = "java(java.time.LocalDateTime.now())"),
    })
    SysRole toSysRole(AddRoleReq data);

    @Mappings({
            @Mapping(source = "id", target = "roleId"),
            @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    })
    RoleVo toRoleVo(SysRole data);

    @Mappings({
            @Mapping(source = "id", target = "roleId"),
            @Mapping(target = "statusStr", expression = "java(com.xu.base.enums.StatusEnum.getByStatus(data.getStatus()))"),
            @Mapping(target = "createBy",expression = "java(getUserName(data.getCreateBy(),userNameMap))"),
            @Mapping(target = "createTime",expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmddhhmmss(data.getCreateTime()))"),
            @Mapping(target = "updateBy",expression = "java(getUserName(data.getUpdateBy(),userNameMap))"),
            @Mapping(target = "updateTime",expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmddhhmmss(data.getUpdateTime()))"),
    })
    RoleSimpleVo toRoleSimpleVo(SysRole data, @Context Map<Long,String> userNameMap);

    List<RoleSimpleVo> toRoleSimpleVoList(List<SysRole> list, @Context Map<Long,String> userNameMap);

    PageInfo<RoleSimpleVo> toRoleSimpleVoPage(PageInfo<SysRole> pageData, @Context Map<Long,String> userNameMap);

    @Mappings({
            @Mapping(source = "id", target = "roleId")
    })
    CurrentRole toCurrentRole(SysRole data);

    List<CurrentRole> toCurrentRoleList(List<SysRole> list);

    /**
     * 翻译创建人和更新人
     * @param byId
     * @param nameMap
     * @return
     */
    default String getUserName(Long byId, Map<Long,String> nameMap) {
        if (nameMap.containsKey(byId)){
            return nameMap.get(byId);
        }
        return "";
    }
}
